package com.example.music.ui.personal;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.room.Room;

import com.example.music.MainActivity;
import com.example.music.databinding.FragmentPersonalBinding;
import com.example.music.room.AppDatabase;
import com.example.music.room.User;
import com.example.music.ui.LoginActivity;


public class PersonalFragment extends Fragment {

    private PersonalViewModel dashboardViewModel;
    private FragmentPersonalBinding binding;
    private User user;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this).get(PersonalViewModel.class);
        binding = FragmentPersonalBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        binding.login.setOnClickListener(v ->{
            startActivity(new Intent(getContext(),LoginActivity.class));
        });
        ((MainActivity) requireActivity()).visibleAll();
        ((MainActivity) requireActivity()).gone();
        return root;
    }

    private Handler handler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 0) {
              binding.name.setEnabled(false);
                binding.login.setVisibility(View.VISIBLE);
                binding.name.setText("请进行登录");
                ((MainActivity) requireActivity()).visible(View.VISIBLE);
            }else if(msg.what == 1){
                User user = (User) msg.obj;
                binding.name.setEnabled(true);
                binding.login.setVisibility(View.GONE);
                binding.name.setText(user.getTag());
                ((MainActivity) requireActivity()).visible(View.GONE);
            }
            return false;
        }
    });

    @Override
    public void onResume() {
        super.onResume();
        AppDatabase db = Room.databaseBuilder(getContext(), AppDatabase.class, "database-name").build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                user = db.userDao().findByOnline(true);
                if(user==null){
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
                }else {
                    Message message = new Message();
                    message.what = 1;
                    message.obj = user;
                    handler.sendMessage(message);
                }
            }
        }).start();
        binding.logout.setOnClickListener(view -> {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if(user==null) return;
                    user.setOnline(false);
                    db.userDao().updateUser(user);
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
            }}).start();
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        AppDatabase db = Room.databaseBuilder(getContext(), AppDatabase.class, "database-name").build();
        new Thread(new Runnable() {
            @Override
            public void run() {
                user = db.userDao().findByOnline(true);
                if(user==null) return;
                user.setTag(binding.name.getText().toString());
                db.userDao().updateUser(user);
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}